package org.afi.mushroomplayer;

import java.io.IOException;
import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

public class PlayService extends Service {

    private static final String TAG = "PlayService";
    public static final String INTENT_NAME_URL_LIST = "intent_name_url_list";
    public static final String INTENT_NAME_SONG_INDEX = "intent_name_song_index";
    public static final String INTENT_TYPE = "intent_type";
    
    public static final int TYPE_SET_LIST = 1;
    public static final int TYPE_PLAY_MUSIC = 2;
    public static final int TYPE_PAUSE_MUSIC = 3;
    public static final int TYPE_LAST_SONG = 4;
    public static final int TYPE_NEXT_SONG = 5;
    private MediaPlayer mPlayer;
    
    private List<String> mUrlList;
    private int mPlayIndex;
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand");
        switch(intent.getExtras().getInt(INTENT_TYPE)) {
        case TYPE_SET_LIST:
            handleSetList(intent);
            break;
        case TYPE_PLAY_MUSIC:
            handlePlayMusic();
            break;
        case TYPE_PAUSE_MUSIC:
            handlePauseMusic();
            break;
        case TYPE_LAST_SONG:
            handleLastSong();
            break;
        case TYPE_NEXT_SONG:
            handleNextSong();
            break;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }
    
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    
    private void handleNextSong() {
        // TODO Auto-generated method stub
        
    }

    private void handleLastSong() {
        // TODO Auto-generated method stub
        
    }

    private void handlePauseMusic() {
        mPlayer.pause();
        Constants.isPlaying = false;
    }

    private void handlePlayMusic() {
        mPlayer.start();
        Constants.isPlaying = true;
    }
    
    private void handleSetList(Intent intent) {
        mUrlList = intent.getStringArrayListExtra(INTENT_NAME_URL_LIST);
        mPlayIndex = intent.getExtras().getInt(INTENT_NAME_SONG_INDEX);
        Log.i(TAG, "index = "+mPlayIndex);
        if (mUrlList != null) {
            for (int i = 0; i < mUrlList.size(); i++) {
                Log.i(TAG, "URL = "+ mUrlList.get(i));
            }    
        }
        if (mPlayer != null && mPlayer.isPlaying()) {
            mPlayer.stop();
            mPlayer.release();
        }
        
        mPlayer = null;
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mUrlList.get(mPlayIndex));
            mPlayer.prepare();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        if (Constants.isPlaying) {
            handlePlayMusic();
        }
    }
    
}
